package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mapper.ArticlesMapper;
import com.itheima.mapper.LoginMapper;
import com.itheima.pojo.HmArticles;
import com.itheima.pojo.PePermissionGroup;
import com.itheima.service.ArticlesService;
import com.itheima.service.LoginService;
import com.itheima.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.Year;
import java.util.Date;
import java.util.List;

@Service
public class ArticlesServiceImpl implements ArticlesService {
    @Autowired
    private ArticlesMapper articlesMapper;
    @Autowired
    private LoginMapper loginMapper;
        @Override
    public PageResult findPage(Integer page, Integer pagesize, String keyword, Integer state) throws ParseException {
        Page<HmArticles> p = new Page<>(page,pagesize);
        LambdaQueryWrapper<HmArticles> wrapper = new LambdaQueryWrapper<>();
        if(keyword!=null){
            wrapper.like(HmArticles::getTitle,keyword);
        }
        if(state!=null){
            wrapper.eq(HmArticles::getState,state);
        }
        Page<HmArticles> hmArticlesPage = articlesMapper.selectPage(p, wrapper);
        Integer count = articlesMapper.selectCount(wrapper);
        int i = count % pagesize;
        Integer pages=0;
        if(i==0){
            pages = count / pagesize;
        }else{
            pages = ((count / pagesize)+1);
        }
        List<HmArticles> records = hmArticlesPage.getRecords();
        List<PePermissionGroup> groups = loginMapper.selectpe_permission_group();
        for (HmArticles ha:records) {
            for (PePermissionGroup ppg:groups) {
                if((ha.getCreatorid()+"").equals(""+ppg.getId())){
                    ha.setUsername(ppg.getName());
                }
            }
            System.out.println(ha);
        }
        return new PageResult(count,records,null,page+"",pages,pagesize+"");
    }

    @Override
    public Integer add(HmArticles hmArticles,String s) {
        Integer creatorId = articlesMapper.selectCreatorid(s);
        hmArticles.setCreatorid(creatorId);
        hmArticles.setVisits(0);
        hmArticles.setCreateTime(new Date());
        articlesMapper.insert(hmArticles);
        return hmArticles.getId();
    }

    @Override
    public boolean Update(Integer id, Integer state) {
        HmArticles hmArticles = articlesMapper.selectById(id);
        hmArticles.setState(state);
        int updateById = articlesMapper.updateById(hmArticles);
        if(updateById == 1){
            return true;
        }
        return false;
    }

    @Override
    public boolean delete(Integer id) {
        int deleteById = articlesMapper.deleteById(id);
        if(deleteById == 1){
            return true;
        }
        return false;
    }

    @Override
    public boolean updateWen(Integer id, HmArticles hmArticles) {
        hmArticles.setId(id);
        int updateById = articlesMapper.updateById(hmArticles);
        if(updateById == 1){
            return true;
        }
        return false;
    }

    @Override
    public HmArticles find(Integer id) {
        HmArticles hmArticles = articlesMapper.selectById(id);
        return hmArticles;
    }
}
